package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryAchievementTypeGradeRelationshipDetailReq;
import com.sg.dto.base.app.req.QueryAchievementTypeGradeRelationshipListReq;
import com.sg.entity.OmsAchievementTypeGradeRelationship;
import com.sg.service.base.app.MOmsAchievementTypeGradeRelationshipService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementTypeGradeRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementTypeGradeRelationshipServiceImpl
        extends ServiceImpl<OmsAchievementTypeGradeRelationshipMapper, OmsAchievementTypeGradeRelationship>
        implements MOmsAchievementTypeGradeRelationshipService {

    @Autowired
    OmsAchievementTypeGradeRelationshipMapper omsAchievementTypeGradeRelationshipMapper;

    /**
     * 3-10-05新增成果类型等级关系[9116]
     * gen by moon at 4/3/2024, 6:42:50 PM
     */
    @Trace(operationName = "3-10-05新增成果类型等级关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementTypeGradeRelationship(OmsAchievementTypeGradeRelationship omsAchievementTypeGradeRelationship) {
        omsAchievementTypeGradeRelationship.setTypeGradeRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementTypeGradeRelationship.setOrderNumber(Long.valueOf(0));//CUSTOM_CONVENTION
        omsAchievementTypeGradeRelationship.setIsArchive("FALSE");
        omsAchievementTypeGradeRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementTypeGradeRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTypeGradeRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementTypeGradeRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementTypeGradeRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTypeGradeRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementTypeGradeRelationship.setIsValid("FALSE");
        if (super.save(omsAchievementTypeGradeRelationship)) {
            return omsAchievementTypeGradeRelationship.getTypeGradeRelationshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-05查询成果类型等级关系列表[9118]
     * gen by moon at 4/3/2024, 6:42:51 PM
     */
    @Trace(operationName = "3-10-05查询成果类型等级关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementTypeGradeRelationship> queryAchievementTypeGradeRelationshipList(QueryAchievementTypeGradeRelationshipListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementTypeGradeRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementTypeGradeRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementTypeGradeRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementTypeGradeRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAchievementTypeGradeRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-05删除成果类型等级关系[9117]
     * gen by moon at 4/3/2024, 6:42:51 PM
     */
    @Trace(operationName = "3-10-05删除成果类型等级关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementTypeGradeRelationship(String typeGradeRelationshipId) {

        return super.removeById(typeGradeRelationshipId);
    }

    /**
     * 3-10-05查询成果类型等级关系详情[9182]
     * gen by moon at 4/3/2024, 6:42:54 PM
     */
    @Trace(operationName = "3-10-05查询成果类型等级关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementTypeGradeRelationship queryAchievementTypeGradeRelationshipDetail(QueryAchievementTypeGradeRelationshipDetailReq reqDto) {

        try {

            QueryWrapper<OmsAchievementTypeGradeRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementTypeGradeRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementTypeGradeRelationship retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-05修改成果类型等级关系[9190]
     * gen by moon at 4/3/2024, 6:42:55 PM
     */
    @Trace(operationName = "3-10-05修改成果类型等级关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementTypeGradeRelationship(OmsAchievementTypeGradeRelationship omsAchievementTypeGradeRelationship) {
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTypeGradeRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTypeGradeRelationship.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTypeGradeRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementTypeGradeRelationship != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle() != null && omsAchievementTypeGradeRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTypeGradeRelationship.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementTypeGradeRelationship);
    }

    /**
     * 3-10-05批量改成果类型等级关系[9203]
     * gen by moon at 4/3/2024, 6:42:56 PM
     */
    @Trace(operationName = "3-10-05批量改成果类型等级关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateAchievementTypeGradeRelationship(List<OmsAchievementTypeGradeRelationship> omsAchievementTypeGradeRelationship) {

        if (omsAchievementTypeGradeRelationship == null || omsAchievementTypeGradeRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsAchievementTypeGradeRelationship);

    }
}
